﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public class hSkill
    {
        Vector2 _src;
        Vector2 _dest;
        hSprite _fireStrom;
        bool _onCast = false;
        private int _damage = 0;
        public bool OnCast
        {
            get { return _onCast; }
            set { _onCast = value; }
        }

        public Vector2 Source
        {
            get { return _src; }
            set { _src = value; }
        }

        public Vector2 Dest
        {
            get { return _dest; }
            set { _dest = value; }
        }

        public hSkill(ContentManager content)
        {
            Texture2D[] txt = new Texture2D[4];
            txt[0] = content.Load<Texture2D>(hGlobal.PathSkill + "Skill_firestrom");
            txt[1] = content.Load<Texture2D>(hGlobal.PathSkill + "Skill_firestrom2");
            txt[2] = content.Load<Texture2D>(hGlobal.PathSkill + "Skill_firestrom3");
            txt[3] = content.Load<Texture2D>(hGlobal.PathSkill + "Skill_firestrom4");
            _fireStrom = new hSprite(txt);
        }

        public void FireStrom(Vector2 src, Vector2 dest, int damage)
        {
            if (_onCast == false)
            {
                _onCast = true;
                Source = src;
                Dest = dest;
                _damage = damage;
                _fireStrom.centerX = Source.X;
                _fireStrom.centerY = Source.Y;
            }
        }

        public void update(hParams args, GameTime gtime)
        {
            if (_onCast)
            {
                if (_fireStrom.centerX < Dest.X)
                {
                    _fireStrom.centerX += 10;
                    if (_fireStrom.centerX > Dest.X)
                        _fireStrom.centerX = Dest.X;
                }

                if (_fireStrom.centerX > Dest.X)
                {
                    _fireStrom.centerX -= 10;
                    if (_fireStrom.centerX < Dest.X)
                        _fireStrom.centerX = Dest.X;
                }

                if (_fireStrom.centerY < Dest.Y)
                {
                    _fireStrom.centerY += 10;
                    if (_fireStrom.centerY > Dest.Y)
                        _fireStrom.centerY = Dest.Y;
                }

                if (_fireStrom.centerY > Dest.Y)
                {
                    _fireStrom.centerY -= 10;
                    if (_fireStrom.centerY < Dest.Y)
                        _fireStrom.centerY = Dest.Y;
                }

                _fireStrom.update(args, gtime);

                if (_fireStrom.centerX == Dest.X && _fireStrom.centerY == Dest.Y)
                {
                    // cast xong, kiểm tra mục tiêu, gây sát thương
                    for (int i = 0; i < args.StructureManager.NRows; i++)
                    {
                        for (int j = 0; j < args.StructureManager.NColumns; j++)
                        {
                            if (args.StructureManager.Structures[i, j] != null)
                            {
                                if (Math.Abs(args.StructureManager.Structures[i, j].Center.X - _fireStrom.centerX) < args.StructureManager.Structures[i, j].Width/2 + _fireStrom.Width/2
                                    && Math.Abs(args.StructureManager.Structures[i, j].Center.Y - _fireStrom.centerY) < args.StructureManager.Structures[i, j].Height / 2 + _fireStrom.Height / 2)
                                {
                                    args.StructureManager.Structures[i, j].Power -= _damage;
                                }
                            }
                        }
                    }
                    for (int i = 0; i < args.UnitManager.Units.Count; i++)
                    {
                        if (Math.Abs(args.UnitManager.Units[i].Center.X - _fireStrom.centerX) < args.UnitManager.Units[i].Width / 2 + _fireStrom.Width / 2
                                && Math.Abs(args.UnitManager.Units[i].Center.Y - _fireStrom.centerY) < args.UnitManager.Units[i].Height / 2 + _fireStrom.Height / 2)
                        {
                            args.UnitManager.Units[i].Power -= _damage;
                        }
                    }
                    _onCast = false;
                }
            }
        }

        public void draw(GameTime gtime, SpriteBatch sb)
        {
            if (_onCast)
            {
                _fireStrom.draw(gtime, sb);
            }
        }
    }
}
